/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.service.impl;

import cc.altius.dao.CustomerDao;
import cc.altius.exception.CouldNotSaveToDataBaseException;
import cc.altius.model.*;
import cc.altius.service.CustomerService;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author sagar
 */
@Service("CustomerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public List<Map<String, Object>> searchCustomer(String mrNo, String phone, String customerName, String empId1) {
        return this.customerDao.searchCustomer(mrNo, phone, customerName, empId1);
    }

    @Override
    public List<PaymentType> getPaymentTypeList() {
        return this.customerDao.getPaymentTypeList();
    }

    @Override
    public Customer getCustomer(String phone, String mrNo, String customerName) {
        return this.customerDao.getCustomer(phone, mrNo, customerName);
    }

    @Override
    public void updateCustomer(Customer customer, Date transFollowupDate, String transNotes, String transCheifComplaint, int callTypeId, int dispositionId, String area, String recordingId, int hospitalId, int appSpecialistId) {
        this.customerDao.updateCustomer(customer, transFollowupDate, transNotes, transCheifComplaint, callTypeId, dispositionId, area, recordingId, hospitalId, appSpecialistId);
    }

    @Override
    public List<TransCustomer> getCustomerTransDetails(int customerId) {
        return this.customerDao.getCustomerTransDetails(customerId);
    }

    @Override
    public List<Map<String, Object>> getFollowupList(int dispositionId) {
        return this.customerDao.getFollowupList(dispositionId);
    }

    @Override
    public List<Relation> getRelationList() {
        return this.customerDao.getRelationList();
    }

    @Override
    public List<CallType> getCallTypeList() {
        return this.customerDao.getCallTypeList();
    }

    @Override
    public List<Disposition> getDispositionList() {
        return this.customerDao.getDispositionList();
    }

//    @Override
//    public int addCustomer(Customer customer, String recordingId, int hospitalId, int appSpecialistId) {
//        return this.customerDao.addCustomer(customer, recordingId, hospitalId, appSpecialistId);
//    }
    @Override
    public Customer getCustomerDetails(int customerId) {
        return this.customerDao.getCustomerDetails(customerId);
    }

    @Override
    public List<Illness> getIllnessList() {
        return this.customerDao.getIllnessList();
    }

    @Override
    public int addIllness(int customerId, String selectedIllnessIds) {
        return this.customerDao.addIllness(customerId, selectedIllnessIds);
    }

    @Override
    public List<Map<String, Object>> getIllnessListByCustomerId(int customerId) {
        return this.customerDao.getIllnessListByCustomerId(customerId);
    }

    @Override
    public List<Map<String, Object>> getIllnessListForLeftTable(int customerId) {
        return this.customerDao.getIllnessListForLeftTable(customerId);
    }

    @Override
    public void updateIllness(int customerId, String selectedIllnessIds) {
        this.customerDao.updateIllness(customerId, selectedIllnessIds);
    }

    @Override
    public List<Map<String, Object>> getCallLogReport(String startDate, String stopDate, int callTypeId, int dispositionId, int doctorId, int userId) {
        return this.customerDao.getCallLogReport(startDate, stopDate, callTypeId, dispositionId, doctorId, userId);
    }

    @Override
    public List<Disposition> getDispositionList(int callTypeId) {
        return this.customerDao.getDispositionList(callTypeId);
    }

    @Override
    public Customer getCustomerListOnEmployeeId(int customerId) {
        return this.customerDao.getCustomerListOnEmployeeId(customerId);
    }

    @Override
    public void updateCustomerForOnlineRegistration(Customer customer) {
        this.customerDao.updateCustomerForOnlineRegistration(customer);
    }

    @Override
    public int addCustomer(Customer customer) {
        return this.customerDao.addCustomer(customer);
    }

    @Override
    public List<Event> getEventList(int corporateId) {
        return this.customerDao.getEventList(corporateId);
    }

    @Override
    public List<CorporateCustomer> getCorporateCustomerList(String customerName, int corporateId) {
        return this.customerDao.getCorporateCustomerList(customerName, corporateId);
    }

    @Override
    public CorporateCustomerEvent getCorporateCustomerEventData(int coCustomerId) {
        return this.customerDao.getCorporateCustomerEventData(coCustomerId);
    }

    @Override
    public int editCorporateCustomerEventData(CorporateCustomerEvent coCustomerEvents) {
        return this.customerDao.editCorporateCustomerEventData(coCustomerEvents);
    }

    @Override
    public List<Map<String, Object>> getCorporateCustomerExcelData(String customerName, int corporateId) {
        return this.customerDao.getCorporateCustomerExcelData(customerName, corporateId);
    }

    @Override
    public Map<String, Object> getCorporateCustomer(FileUpload fileUpload) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<CorporateCustomer> corporateCustomerList = new LinkedList<CorporateCustomer>();
        List<CorporateCustomerEvent> corporateCustomerEventList = new LinkedList<CorporateCustomerEvent>();
        String row = "";
        String[] dataFields;
        int line = 0;
        try {
            CorporateCustomer corporateCustomer = null;
            CorporateCustomerEvent corporateCustomerEvent = null;
            PaymentType corporate = null;
            BufferedReader br = new BufferedReader(new InputStreamReader(fileUpload.getMultipartFile().getInputStream()));
            SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
            while ((row = br.readLine()) != null) {
                line = line + 1;
                if (row.equals("")) {
                    break;
                }
                dataFields = row.split(",");
                if (dataFields != null) {
                    if (dataFields.length == 56) {
                        corporateCustomer = new CorporateCustomer();
                        corporateCustomerEvent = new CorporateCustomerEvent();
                        corporate = null;

                        /*
                         * set corporate customer personal data
                         */
                        String str = dataFields[0].trim();
                        corporateCustomer.setEmployeeId(str);

                        str = dataFields[1].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setName(null);
                        } else {
                            corporateCustomer.setName(str);
                        }

                        str = dataFields[2].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setOfficeLocation(null);
                        } else {
                            corporateCustomer.setOfficeLocation(str);
                        }

                        str = dataFields[3].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setDepartment(null);
                        } else {
                            corporateCustomer.setDepartment(str);
                        }

                        str = dataFields[4].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setDesignation(null);
                        } else {
                            corporateCustomer.setDesignation(str);
                        }

                        str = dataFields[5].trim().replace(" ", "");
                        String[] phones = str.split("/");
                        str = "";
                        int countPhone = 0;
                        for (String phone : phones) {
                            if (!phone.isEmpty()) {
                                if (countPhone > 0) {
                                    str += "/";
                                }
                                if (phone.length() == 10) {
                                    str += phone;
                                    countPhone++;

                                }
                            }
                        }
                        corporateCustomer.setPhone(str);

                        str = dataFields[6].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setEmail(null);
                        } else {
                            corporateCustomer.setEmail(str);
                        }

                        str = dataFields[7].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setCategoryA(null);
                        } else {
                            corporateCustomer.setCategoryA(str);
                        }

                        str = dataFields[8].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setCategoryB(null);
                        } else {
                            corporateCustomer.setCategoryB(str);
                        }

                        str = dataFields[9].trim();
                        if (str.isEmpty()) {
                            corporateCustomer.setCategoryC(null);
                        } else {
                            corporateCustomer.setCategoryC(str);
                        }

                        str = dataFields[10].trim();
                        corporate = this.customerDao.getCorporateById(Integer.parseInt(str));
                        corporateCustomer.setCorporate(corporate);
                        /*
                         * end
                         */

                        /*
                         * set Event data for corporate customer
                         */
                        List<Event> events = this.customerDao.getEventList(corporate.getPaymentTypeId());

                        for (Event e : events) {
                            //event1
                            if (e.getEventId() == Integer.parseInt(dataFields[11].trim())) {
                                corporateCustomerEvent.setEvent1(e);
                                str = dataFields[12].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent1(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent1(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent1(null);
                                }

                                str = dataFields[13].trim();
                                try {
                                    corporateCustomerEvent.setStatus1(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus1(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[14].trim())) {
                                corporateCustomerEvent.setEvent2(e);
                                //event2
                                str = dataFields[15].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent2(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent2(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent2(null);
                                }

                                str = dataFields[16].trim();
                                try {
                                    corporateCustomerEvent.setStatus2(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus2(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[17].trim())) {
                                corporateCustomerEvent.setEvent3(e);
                                //event3
                                str = dataFields[18].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent3(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent3(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent3(null);
                                }

                                str = dataFields[19].trim();
                                try {
                                    corporateCustomerEvent.setStatus3(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus3(0);
                                }
                            } else if (e.getEventId() == Integer.parseInt(dataFields[20].trim())) {
                                corporateCustomerEvent.setEvent4(e);
                                //event4
                                str = dataFields[21].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent4(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent4(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent4(null);
                                }

                                str = dataFields[22].trim();
                                try {
                                    corporateCustomerEvent.setStatus4(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus4(0);
                                }
                            } else if (e.getEventId() == Integer.parseInt(dataFields[23].trim())) {
                                corporateCustomerEvent.setEvent5(e);
                                //event5
                                str = dataFields[24].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent5(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent5(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent5(null);
                                }

                                str = dataFields[25].trim();
                                try {
                                    corporateCustomerEvent.setStatus5(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus5(0);
                                }
                            } else if (e.getEventId() == Integer.parseInt(dataFields[26].trim())) {
                                corporateCustomerEvent.setEvent6(e);
                                //event6
                                str = dataFields[27].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent6(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent6(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent6(null);
                                }

                                str = dataFields[28].trim();
                                try {
                                    corporateCustomerEvent.setStatus6(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus6(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[29].trim())) {
                                corporateCustomerEvent.setEvent7(e);
                                //event7
                                str = dataFields[30].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent7(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent7(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent7(null);
                                }

                                str = dataFields[31].trim();
                                try {
                                    corporateCustomerEvent.setStatus7(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus7(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[32].trim())) {
                                corporateCustomerEvent.setEvent8(e);
                                //event8
                                str = dataFields[33].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent8(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent8(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent8(null);
                                }

                                str = dataFields[34].trim();
                                try {
                                    corporateCustomerEvent.setStatus8(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus8(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[35].trim())) {
                                corporateCustomerEvent.setEvent9(e);
                                //event9
                                str = dataFields[36].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent9(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent9(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent9(null);
                                }

                                str = dataFields[37].trim();
                                try {
                                    corporateCustomerEvent.setStatus9(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus9(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[38].trim())) {
                                corporateCustomerEvent.setEvent10(e);
                                //event10
                                str = dataFields[39].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent10(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent10(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent10(null);
                                }

                                str = dataFields[40].trim();
                                try {
                                    corporateCustomerEvent.setStatus10(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus10(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[41].trim())) {
                                corporateCustomerEvent.setEvent11(e);
                                //event11
                                str = dataFields[42].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent11(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent11(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent11(null);
                                }

                                str = dataFields[43].trim();
                                try {
                                    corporateCustomerEvent.setStatus11(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus11(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[44].trim())) {
                                corporateCustomerEvent.setEvent12(e);
                                //event12
                                str = dataFields[45].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent12(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent12(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent12(null);
                                }

                                str = dataFields[46].trim();
                                try {
                                    corporateCustomerEvent.setStatus12(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus12(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[47].trim())) {
                                corporateCustomerEvent.setEvent13(e);
                                //event13
                                str = dataFields[48].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent13(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent13(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent13(null);
                                }

                                str = dataFields[49].trim();
                                try {
                                    corporateCustomerEvent.setStatus13(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus13(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[50].trim())) {
                                corporateCustomerEvent.setEvent14(e);
                                //event14
                                str = dataFields[51].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent14(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent14(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent14(null);
                                }

                                str = dataFields[52].trim();
                                try {
                                    corporateCustomerEvent.setStatus14(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus14(0);
                                }

                            } else if (e.getEventId() == Integer.parseInt(dataFields[53].trim())) {
                                corporateCustomerEvent.setEvent15(e);
                                //event15
                                str = dataFields[54].trim();
                                try {
                                    if (str.isEmpty()) {
                                        corporateCustomerEvent.setScheduleEvent15(null);
                                    } else {
                                        corporateCustomerEvent.setScheduleEvent15(dateFormatter.parse(str));
                                    }
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setScheduleEvent15(null);
                                }

                                str = dataFields[55].trim();
                                try {
                                    corporateCustomerEvent.setStatus15(Integer.parseInt(str));
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                    corporateCustomerEvent.setStatus15(0);
                                }
                            } else {
                                throw new CouldNotSaveToDataBaseException("Incorrect event on " + line + " customer record");
                            }
                        }

                        /*
                         * end
                         */

                        corporateCustomerList.add(corporateCustomer);
                        corporateCustomerEventList.add(corporateCustomerEvent);
                    } else {
                        throw new CouldNotSaveToDataBaseException("Incorrect number of parameters on line no " + line);
                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new CouldNotSaveToDataBaseException(e);
        }
        map.put("corporateCustomerList", corporateCustomerList);
        map.put("corporateCustomerEventList", corporateCustomerEventList);
        return map;
    }

    @Override
    public int saveCorporateCustomerData(List<CorporateCustomer> corporateCustommerList, List<CorporateCustomerEvent> corporateCustommerEventList) {
        return this.customerDao.saveCorporateCustomerData(corporateCustommerList, corporateCustommerEventList);
    }

    @Override
    public boolean updateEvent(int corporateId, int eventId, String eventDate, int eventStatus) {
        return this.customerDao.updateEvent(corporateId, eventId, eventDate, eventStatus);
    }

    @Override
    public boolean editEvent(Event event) {
        return this.customerDao.editEvent(event);
    }

    @Override
    public int addCorporate(PaymentType corporate) {
        return this.customerDao.addCorporate(corporate);
    }

    @Override
    public PaymentType getCorporateById(int corporateId) {
        return this.customerDao.getCorporateById(corporateId);
    }

    @Override
    public int editCorporate(PaymentType corporate) {
        return this.customerDao.editCorporate(corporate);
    }

    @Override
    public int addCorporateCustomer(CorporateCustomer corporateCustomer, CorporateCustomerEvent customerEvent) {
        return this.customerDao.addCorporateCustomer(corporateCustomer, customerEvent);
    }

    @Override
    public CorporateCustomer getCorporateCustomerById(int corporateCustomerId) {
        return this.customerDao.getCorporateCustomerById(corporateCustomerId);
    }

    @Override
    public int editCorporateCustomer(CorporateCustomer corporateCustomer) {
        return this.customerDao.editCorporateCustomer(corporateCustomer);
    }

    @Override
    public List<EventStatus> getEventStatusList() {
        return this.customerDao.getEventStatusList();
    }
}
